package cn.com.dstz.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.hibernate.type.Type;

public class DataBaseUtils{

	public static String getReplaceSQL( String tableName, Map< String, Object > replaceData ){
		String comma = "";
		StringBuffer insertKey = new StringBuffer ( );
		StringBuffer insertValue = new StringBuffer ( );
		for ( Entry < String, Object > entry : replaceData.entrySet ( ) ) {
			insertKey.append ( comma + "`" + entry.getKey ( ) + "`" );
			insertValue.append ( comma + "'" + entry.getValue ( ) + "'" );
			comma = ", ";
		}
		String sql = "replace into " + tableName+ " (" + insertKey + ") values (" + insertValue + ")";
		
		return sql;
	}
	public static String getInsertSQL( String tableName, Map< String, Object > insertData ){
		String comma = "";
		StringBuffer insertKey = new StringBuffer ( );
		StringBuffer insertValue = new StringBuffer ( );
		for ( Entry < String, Object > entry : insertData.entrySet ( ) ) {
			insertKey.append ( comma + "`" + entry.getKey ( ) + "`" );
			insertValue.append ( comma + "'" + entry.getValue ( ) + "'" );
			comma = ", ";
		}
		String sql = "insert into " + tableName+ " (" + insertKey + ") values (" + insertValue + ")";
		
		return sql;
	}
	
	public static Object []  getObjectArray( Object ... obj ){
		Object [] objs = new Object [ obj.length ];
		
		for( int i = 0; i < obj.length; i++ ){
			objs[ i ] = obj[i];
		}
		return objs;
	}
	
	public static Type [] getTypeArray( Type ... type ){
		Type [] types = new Type [ type.length ];
		
		for( int i = 0; i < type.length; i++ ){
			types[ i ] = type[i];
		}
		return types;
	}
	
	public static String [ ] getStringArray( String ... key ){
		String [] strs = new String [ key.length ];
		
		for( int i = 0; i < key.length; i++ ){
			strs[ i ] = key[i];
		}
		return strs;
	}
	
	public static Map< String, Type > getAlias( String [] keys, Type [] values ){
		Map< String, Type > data = new HashMap< String, Type >( );
		for( int i = 0; i < keys.length; i++ ){
			data.put( keys[ i ], values[ i ] );
		}
		return data;
	}
	
	public static Map< String, Object > getData( String [] keys, Object [] values ){
		Map< String, Object > data = new HashMap< String, Object >( );
		
		for( int i = 0; i < keys.length; i++ ){
			data.put( keys[ i ], values[ i ] );
		}
		
		return data;
	}
	
	public static Map< String, Object > getInsertData( String [] keys, Object [] values ){
		return getData( keys, values );
	}
	
	public static void main( String [ ] args ) {
		getStringArray( "a", "b", "c", "d ");
		
		
	}
}
